package com.chixing.service.impl;

import com.alibaba.fastjson.JSON;
import com.chixing.pojo.RequestMessage;
import com.chixing.service.RequestMessageService;
import com.chixing.server.sender.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RequestMessageServiceImpl implements RequestMessageService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private Sender sender;
    @Override
    public void sendMessageByRoom(Integer custId) {
        RequestMessage requestMessageExample = new RequestMessage();
        requestMessageExample.setStatus(0);// 查询状态为0的消息
        requestMessageExample.setRoom(custId.toString());
        Query query = new Query(Criteria.byExample(requestMessageExample));
        List<RequestMessage> requestMessageList = mongoTemplate.find(query,RequestMessage.class);
//        Query query = new Query(Criteria.wh);
        for (RequestMessage requestMessage : requestMessageList){
            sender.send(JSON.toJSONString(requestMessage));
        }
        // 将之前未推送成功的信息status设置为1
        Update update = new Update().set("status",1);
        mongoTemplate.updateMulti(query,update,RequestMessage.class);

    }
}
